window.addEventListener("load", () => {
  // 1. 获取从查询页传过来的快递单号
  function getQueryString(number) {
    const REG = new RegExp("(^|&)" + number + "=([^&]*)(&|$)", "i");
    const RESULT = window.location.search.substr(1).match(REG);
    return RESULT ? RESULT[2] : null;
  }

  const ORDER_NUMBER = decodeURIComponent(getQueryString("orderNumber"));
  ORDER_NUMBER !== "null" && getList();

  async function getList() {
    const res = await request({
      url: `/findLogisticsInfo/${ORDER_NUMBER}`
    });

    const INFO = res.data.logisticsInformationVO;
    const MSG = res.data.logisticsInfoVOS;

    // 3.基本信息
    const DETAILSEl = document.querySelector(".details");
    const ADDRESSEl = document.querySelector(".address_item");
    const STATUSEL = document.querySelector(".status_remark");
    const COMPANYEL = document.querySelector(".company");
    const ORDER_NUMBEREL = document.querySelector(".number");
    const NAMEEL = document.querySelector(".name");
    const PHONEEL = document.querySelector(".phone_number");

    // 客服电话
    const CALL_NUMBER = INFO.serviceTel;
    const CALL_NUMBEREL = document.querySelector(".call-number");
    CALL_NUMBEREL.href = "tel:" + CALL_NUMBER;

    const COPYEl = document.querySelector(".copy");
    COPYEl.setAttribute("data-order-number", ORDER_NUMBER);

    STATUSEL.textContent = INFO.statusRemark;
    COMPANYEL.textContent = INFO.company;
    ORDER_NUMBEREL.textContent = ORDER_NUMBER;
    NAMEEL.textContent = INFO.consignee;
    PHONEEL.textContent = INFO.phone;
    ADDRESSEl.textContent = INFO.address;

    // 2.返回查询页并回传手机号
    const BACKEL = document.querySelector(".back ");
    function backHistory() {
      window.location.href = "index.html?phone=" + PHONEEL.innerHTML;
    }
    BACKEL.addEventListener("click", backHistory);

    if (MSG.length !== 0) {
      // 4.物流详情
      MSG.sort((a, b) => {
        return new Date(b.acceptTime) - new Date(a.acceptTime);
      });
      MSG.forEach((item) => {
        let status1 = "";
        let status2 = "";
        if (item.statusRemark === "派件中" || item.statusRemark === "派件异常") {
          status1 = "isSend";
          status2 = "paisong";
        }

        if (
          item.statusRemark === "在途中" ||
          item.statusRemark === "暂无轨迹信息" ||
          item.statusRemark === "问题件" ||
          item.statusRemark === "到达派件城市" ||
          item.statusRemark === "已放入快递柜或驿站" ||
          item.statusRemark === "发货无信息" ||
          item.statusRemark === "超时未更新"
        ) {
          status1 = "isGet";
          status2 = "qujian";
        }
        if (item.statusRemark === "待揽件" || item.statusRemark === "已揽收") {
          status1 = "isGet";
          status2 = "qujian";
        }
        if (
          item.statusRemark === "正常签收" ||
          item.statusRemark === "签收" ||
          item.statusRemark === "派件异常后最终签收" ||
          item.statusRemark === "代收签收" ||
          item.statusRemark === "快递柜或驿站签收" ||
          item.statusRemark === "超时未签收" ||
          item.statusRemark === "拒收(退件)" ||
          item.statusRemark === "退货签收" ||
          item.statusRemark === "退货未签收" ||
          item.statusRemark === "快递柜或驿站超时未取"
        ) {
          status1 = "isGet";
          status2 = "qujian";
        }

        // 5.拨打快递员号码
        let acceptStation = item.acceptStation;
        let acceptStation1 = item.acceptStation;
        const REGR = /(1[3|4|5|7|8][\d]{9}|0[\d]{2,3}-[\d]{7,8}|400[-]?[\d]{3}[-]?[\d]{4})/g;
        const PHONE_NUMBER = acceptStation.match(REGR);
        if (PHONE_NUMBER) {
          for (let i = 0; i < PHONE_NUMBER.length; i++) {
            let temp = PHONE_NUMBER[i];
            var regG = new RegExp(temp, "g");
            acceptStation = acceptStation1.replace(
              regG,
              '<a href="tel:' + temp + '"style=" color: blue;">' + temp + "</a>"
            );
          }
        }

        const DIVEL = document.createElement("div");
        DIVEL.innerHTML = `
       <div class="details_item ${status1}">
            <div class="details_time">
              <p class="date ${status1 === "isOK" ? "fs30" : ""}"">${item.acceptTime.slice(5, 10)}</p>
              <p class="date_time">${item.acceptTime.slice(11, 16)}</p>
            </div>
            <div class="msg ${status2}">
              <div class="status">${item.statusRemark}</div>
              <div class="status_msg">
               ${acceptStation}
              </div>
            </div>
          </div>
      `;
        DIVEL.classList = `details_item ${status1}`;
        DETAILSEl.appendChild(DIVEL);
      });

      // 地址与最新物流状态
      const NEW_MSG = DETAILSEl.children[1].children[0];
      NEW_MSG.children[0].children[0].classList = "fs30";

      // 4. 复制快递单号功能
      const COPY = document.querySelector(".copy");
      COPY.addEventListener("click", function () {
        const TEXT = this.getAttribute("data-order-number");
        const FLAG = copyText(TEXT);
        FLAG ? alert("复制成功") : alert("复制失败");
      });
    } else {
      DETAILSEl.innerHTML = "暂无物流信息";
      DETAILSEl.classList = "not";
    }
  }
});
